package com.offcn.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.offcn.common.utils.JWTUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.offcn.member.entity.UmsMemberEntity;
import com.offcn.member.service.UmsMemberService;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员-会员表
 *
 * @author yaoshang
 * @email wuqian@gmail.com
 * @date 2021-07-21 19:50:57
 */
@RestController
@RequestMapping("member/member")
public class UmsMemberController {
    @Resource
    private UmsMemberService umsMemberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = umsMemberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		UmsMemberEntity umsMember = umsMemberService.getById(id);

        return R.ok().put("umsMember", umsMember);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody UmsMemberEntity umsMember){
		umsMemberService.save(umsMember);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UmsMemberEntity umsMember){
		umsMemberService.updateById(umsMember);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		umsMemberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据开始时间,结束时间,统计每日注册账号
     */
    @RequestMapping("/count")
    public R count(String beginTime,String endTime){
        List<Map<String, Object>> mapList = umsMemberService.countMember(beginTime, endTime);
        return R.ok().put("mapList",mapList);
    }

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @return
     */
    @PostMapping("/login")
    public R login(String username,String password){
        UmsMemberEntity memberEntity = umsMemberService.login(username, password);
        if (null != memberEntity){  //如果用户名密码正确，则将token,username,expireTime存入redis缓存中
            //生成token
            String token = JWTUtil.generateToken(memberEntity.getUserName());
            //生成refreshToken  --->生成UUID，去掉中间的"-”,即设置成refreshToken
            String refreshToken = UUID.randomUUID().toString().replace("-", "");
            redisTemplate.opsForHash().put(refreshToken,"token",token);
            redisTemplate.opsForHash().put(refreshToken,"username",memberEntity.getUserName());
            //设置token的过期时间  这里需要long类型的时间，所以常量应该设置为long类型
            redisTemplate.expire(refreshToken,JWTUtil.TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);
            //返回给前端token 和 refreshToken
            return R.ok().put("token",token).put("refreshToken",refreshToken);
        }else {
            return R.error("账号或者密码错误");
        }
    }

    /**
     * 刷新token
     * @return
     */
    @PostMapping("/refreshToken")
    public R refreshToken(String refreshToken){
        //1,根据refreshToken获取用户名
        String username = (String) redisTemplate.boundHashOps(refreshToken).get("username");
        if (StringUtils.isEmpty(username)){
            return R.error("刷新token失败");
        }
        //2,根据用户名生成新的token
        String token = JWTUtil.generateToken(username);
        redisTemplate.boundHashOps(refreshToken).put("token",token);
        return R.ok().put("token",token).put("refreshToken",refreshToken);
    }


}
